from django.db import models
import datetime

# Create your models here.


# 顾客表
class User(models.Model):
    username = models.CharField("账号",max_length=20)
    mobile = models.CharField("手机号", max_length=11)
    password = models.CharField("密码", max_length=100)
    account = models.CharField("用户昵称", max_length=40, default="未设置昵称")
    last_login_time = models.DateTimeField("上次登录", null=True, blank=True)
    member = models.IntegerField("会员等级", null=True,blank=True)
    create_time = models.DateTimeField("创建时间", auto_now_add=True, blank=True)
    update_time = models.DateTimeField("修改时间", auto_now=True, blank=True)
    age = models.IntegerField("年龄", null=True, blank=True)
    sex = models.CharField(default="男", max_length=10, verbose_name="性别", null=True, blank=True)
    portrait_img = models.CharField("头像", max_length=200, null=True, blank=True)

    def __str__(self):
        return self.username

    class Meta:
        db_table='user_tb'
        verbose_name_plural = '顾客表'


# 商家表
class Merchant(models.Model):
    username = models.CharField("商家名称", max_length=50)
    pwd = models.CharField("商家密码", max_length=256)
    mobile = models.CharField("手机号", max_length=11)
    signature = models.CharField("签名", max_length=100, null=True, blank=True)
    address = models.CharField("地址", max_length=100, null=True, blank=True)
    contact = models.CharField("联系方式", max_length=30, null=True, blank=True)
    portrait_img = models.CharField("头像", max_length=200, null=True, blank=True)
    business = models.DateTimeField("营业时间", null=True, blank=True)
    closetime = models.DateTimeField("打烊时间", null=True, blank=True)
    min_price = models.DecimalField("起送费", max_digits=4, decimal_places=2, default=10)

    def __str__(self):
        return self.username

    class Meta:
        db_table = 'merchant_tb'
        verbose_name_plural = '商家表'


# 收货地址表
class Address(models.Model):
    user_id = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='顾客id')
    address = models.CharField("收货地址", max_length=100)

    def __str__(self):
        return self.user_id.username

    class Meta:
        db_table = "address_tb"
        verbose_name_plural = '收货地址表'


# 菜品分类表
class DishesClassify(models.Model):
    dishes_class_name = models.CharField('分类名称', max_length=20)
    merchant_id = models.ForeignKey(Merchant, on_delete=models.CASCADE, verbose_name='商家id')

    def __str__(self):
        return self.dishes_class_name

    class Meta:
        db_table = "dishes_classify_tb"
        verbose_name_plural = "菜品分类表"


# 菜品表
class Dishes(models.Model):
    dishes_name = models.CharField("菜品名称", max_length=30)
    dishes_price = models.DecimalField("菜品价格", max_digits=5,decimal_places=2)
    dishes_img = models.CharField("菜品图片", max_length=500, default="")
    dis_class_id = models.ForeignKey(DishesClassify, on_delete=models.CASCADE, verbose_name="菜品分类id")
    merchant_id = models.ForeignKey(Merchant, on_delete=models.CASCADE, default="", verbose_name='商家id')

    def __str__(self):
        return self.dishes_name

    class Meta:
        db_table = 'dishes_tb'
        verbose_name_plural = '菜品表'


# 购物车表
class Shopping(models.Model):
    user_id = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='顾客id')
    merchant_id = models.ForeignKey(Merchant, on_delete=models.CASCADE, default="", verbose_name='商家id')
    dishes_id = models.ForeignKey(Dishes, on_delete=models.CASCADE, verbose_name='菜id')
    num = models.IntegerField("商品数量", default=1)

    def __str__(self):
        return "%s购买的%s"%(self.user_id.username,self.dishes_id.dishes_name)

    class Meta:
        db_table = 'shopping_tb'
        verbose_name_plural = '购物车表'


# 订单表
class Order(models.Model):
    PAY_STATUS = (
        (1, '待支付'),
        (2, '待发货'),
        (3, '待收货'),
        (4, '待评价'),
        (5, '已完成'),
        (6, '已超时')
    )
    order_id = models.CharField("订单id", max_length=200, default="")
    user_id = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='顾客id')
    address_id = models.ForeignKey(Address, on_delete=models.CASCADE, default="", verbose_name="收货地址id")
    status = models.SmallIntegerField(default=1, choices=PAY_STATUS, verbose_name="订单状态")
    price = models.DecimalField(max_digits=7,decimal_places=2, default=0 ,verbose_name="总价")
    order_num = models.IntegerField("商品数量", default=0)
    merchant = models.ForeignKey(Merchant, on_delete=models.CASCADE, default="", verbose_name='商家id')
    # 0为否  1为是
    is_refund = models.IntegerField("是否退款", default=0)
    payment_time = models.DateTimeField("支付时间", auto_now_add=True)

    def __str__(self):
        return "%s的订单"%self.user_id.username

    class Meta:
        db_table = 'order_tb'
        verbose_name_plural = '订单表'


# 订单菜品表
class OrderGoods(models.Model):
    order_id = models.ForeignKey(Order, on_delete=models.CASCADE, verbose_name='所属订单id')
    user_id = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="所属用户id")
    dishes_id = models.ForeignKey(Dishes, on_delete=models.CASCADE, verbose_name="所属菜品id")
    num = models.IntegerField("商品数量", default=1, null=True, blank=True)
    merchant = models.ForeignKey(Merchant, on_delete=models.CASCADE, default="", verbose_name="所属商家")
    create_time = models.DateTimeField("创建订单时间", auto_now_add=True, blank=True,null=True)
    def __str__(self):
        return "%s的订单信息"%self.user_id.username

    class Meta:
        db_table = 'order_goods_tb'
        verbose_name_plural = '订单菜品表'


# 评论表
class Comment(models.Model):
    user_id = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='顾客id')
    merchant_id = models.ForeignKey(Merchant, on_delete=models.CASCADE, verbose_name='商家id')
    order_id = models.ForeignKey(Order, on_delete=models.CASCADE, verbose_name='订单id')
    content = models.CharField("评论内容", max_length=100)
    user_img = models.CharField("用户头像", max_length=300, default="")
    create_time = models.DateTimeField("发布时间时间", auto_now_add=True, null=True, blank=True)


    def __str__(self):
        return "%s的评论"%self.user_id.username

    class Meta:
        db_table = 'comment_tb'
        verbose_name_plural = "评论表"


# 回复评论表
class CommentReply(models.Model):
    user_id = models.ForeignKey(User, on_delete=models.CASCADE, default="", null=True, blank=True, verbose_name='顾客id')
    merchant_id = models.ForeignKey(Merchant, on_delete=models.CASCADE, default="", null=True, blank=True, verbose_name='商家id')
    comment = models.ForeignKey(Comment, on_delete=models.CASCADE, verbose_name='评论id')
    content = models.CharField("评论内容", max_length=100)
    user_img = models.CharField("用户头像", max_length=300, default="")
    create_time = models.DateTimeField("发布时间时间", auto_now_add=True, null=True, blank=True)

    def __str__(self):
        return self.comment

    class Meta:
        db_table = "comment_reply"
        verbose_name_plural = "评论回复表"


# 回复展示表
class CommentShow(models.Model):
    send_time = models.DateTimeField("发布评论时间", null=True,blank=True,auto_now_add=True)
    user_img = models.CharField("用户头像", max_length=300, default="")
    commentator = models.CharField("回复者", max_length=30, default="")
    be_reviewed = models.CharField("被回复者", max_length=30, default="")
    content = models.CharField("评论内容", max_length=100)

    def __str__(self):
        return "%s回复了%s"%(self.commentator,self.be_reviewed)
    class Meta:
        db_table = "commentshow_tb"
        verbose_name_plural = "回复展示表"


# 优惠券表
class Coupon(models.Model):
    type_name = models.CharField("优惠券类型", max_length=20)
    coupon_name = models.CharField("优惠券名称", max_length=50)
    data_time = models.DateTimeField("开始日期", null=True,blank=True)
    end_time = models.DateTimeField("结束日期", null=True,blank=True)
    money = models.DecimalField("优惠金额", max_digits=5,decimal_places=2, default=0)

    def __str__(self):
        return self.coupon_name

    class Meta:
        db_table = 'coupon_tb'
        verbose_name_plural = "优惠券表"


# 沟通表
class Communication(models.Model):
    user_id = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='顾客id')
    merchant_id = models.ForeignKey(Merchant, on_delete=models.CASCADE, verbose_name='商家id')

    class Meta:
        db_table = 'communication_tb'
        verbose_name_plural = '沟通表'

